#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Datetime: 2023/2/24 16:46
# @Author  : CHENWang
# @Site    : 
# @File    : macro_analysis.py
# @Software: PyCharm

"""
脚本说明:
"""

import os
import time
import datetime
import numpy as np
import pandas as pd
from quant_researcher.quant.project_tool import hammer
from quant_researcher.quant.project_tool.localize import DATA_DIR


if __name__ == '__main__':
    file_path = os.path.join(DATA_DIR, f'macro_data')
    file_name = os.path.join(file_path, 'US_ISM_PMI')
    US_ISM_PMI = pd.read_excel(f'{file_name}.xlsx')
    US_ISM_PMI.rename(columns={'指标名称': 'end_date'}, inplace=True)
    US_ISM_PMI['end_date'] = pd.to_datetime(US_ISM_PMI['end_date'])
    US_ISM_PMI['end_date'] = US_ISM_PMI['end_date'].dt.strftime('%Y-%m-%d')
    US_ISM_PMI.set_index('end_date', inplace=True)

    # 所有数据披露日期
    all_report_date = list(US_ISM_PMI[~US_ISM_PMI['美国:供应管理协会(ISM):制造业PMI'].isnull()].index)

    # US_ISM_PMI指数价格市盈率后填
    US_ISM_PMI.ffill(inplace=True)
    US_ISM_PMI['美国:标准普尔500指数_log'] = np.log10(US_ISM_PMI['美国:标准普尔500指数'])
    US_ISM_PMI['美国:纳斯达克综合指数_log'] = np.log10(US_ISM_PMI['美国:纳斯达克综合指数'])

    US_ISM_PMI['美国:标准普尔500指数_rolling30ret'] = US_ISM_PMI['美国:标准普尔500指数'].pct_change(30)
    US_ISM_PMI['美国:标准普尔500指数_rolling60ret'] = US_ISM_PMI['美国:标准普尔500指数'].pct_change(60)
    US_ISM_PMI['美国:标准普尔500指数_rolling90ret'] = US_ISM_PMI['美国:标准普尔500指数'].pct_change(90)
    US_ISM_PMI['美国:标准普尔500指数_rolling120ret'] = US_ISM_PMI['美国:标准普尔500指数'].pct_change(120)
    US_ISM_PMI['美国:标准普尔500指数_rolling150ret'] = US_ISM_PMI['美国:标准普尔500指数'].pct_change(150)
    US_ISM_PMI['美国:标准普尔500指数_rolling200ret'] = US_ISM_PMI['美国:标准普尔500指数'].pct_change(200)
    US_ISM_PMI['美国:标准普尔500指数_rolling252ret'] = US_ISM_PMI['美国:标准普尔500指数'].pct_change(252)

    US_ISM_PMI['美国:纳斯达克综合指数_rolling30ret'] = US_ISM_PMI['美国:纳斯达克综合指数'].pct_change(30)
    US_ISM_PMI['美国:纳斯达克综合指数_rolling60ret'] = US_ISM_PMI['美国:纳斯达克综合指数'].pct_change(60)
    US_ISM_PMI['美国:纳斯达克综合指数_rolling90ret'] = US_ISM_PMI['美国:纳斯达克综合指数'].pct_change(90)
    US_ISM_PMI['美国:纳斯达克综合指数_rolling120ret'] = US_ISM_PMI['美国:纳斯达克综合指数'].pct_change(120)
    US_ISM_PMI['美国:纳斯达克综合指数_rolling150ret'] = US_ISM_PMI['美国:纳斯达克综合指数'].pct_change(150)
    US_ISM_PMI['美国:纳斯达克综合指数_rolling200ret'] = US_ISM_PMI['美国:纳斯达克综合指数'].pct_change(200)
    US_ISM_PMI['美国:纳斯达克综合指数_rolling252ret'] = US_ISM_PMI['美国:纳斯达克综合指数'].pct_change(252)

    # 截取数据披露日数据
    US_ISM_PMI_df = US_ISM_PMI.loc[all_report_date, :]

    file_name = os.path.join(file_path, 'US_ISM_PMI_analysis')
    US_ISM_PMI_df.to_excel(f'{file_name}.xlsx', index=True)